package secondwork;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name = "loginServlet", urlPatterns = { "/loginServlet" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Customer customer = new Customer();
		customer.setUid(1);
		customer.setName("liushichen");
		customer.setPassword("123");
		String name = req.getParameter("cname");
		String password = req.getParameter("pwd");
		if (name.equals(customer.getName()) && password.equals(customer.getPassword())) {
			Cookie c = new Cookie("name", customer.getName());
			c.setMaxAge(1800 * 3);
			c.setPath("/demo/ck");
			resp.addCookie(c);
			HttpSession session = req.getSession(); // request是HttpServletRequest类型的
			session.setAttribute("name", customer.getName());
			resp.sendRedirect("/demo/main.jsp");
			return;
		} else {
			req.setAttribute("str", "用户名或密码错误");
			req.getRequestDispatcher("/login2.html").forward(req, resp);
		}
	}

}
